This exercise creates a comprehensive shopping cart system that handles user registration, product selection, cart management, and invoice generation with tax calculations. It demonstrates real-world e-commerce functionality.
if opcion == 1: if not usuario: # First-time registration nombre = input("Ingrese su nombre(s) =>") apellido = input("Ingrese su apellido(s) =>") documento = input("Ingrese su numero de documento=>") usuario.append(nombre) usuario.append(apellido) usuario.append(documento) print("Usuario registrado correctamente") else: # Update existing user usuario[0] = nombre usuario[1] = apellido usuario[2] = documento
Why check 'if not usuario'?
An empty list evaluates to False in Python, so if not usuario checks if the list is empty. This pattern determines whether to register a new user or update an existing one.
elif opcion == 3: if not carrito: print("El carrito esta vacio") else: print("carrito actual") subtotal = 0 for i in carrito: print(f"{i[0]}({i[1]}) * {i[2]} = {i[3]}") subtotal += i[3] iva = subtotal * 0.19 total = (subtotal + iva) print("SUBTOTAL =", subtotal) print("IVA 19% =", iva) print("TOTAL =", total)
IVA Calculation: IVA (Impuesto al Valor Agregado) is the Value Added Tax used in many Spanish-speaking countries. In this example, it’s calculated as 19% of the subtotal.
elif opcion == 5: if not carrito: print("El carrito esta vacio") else: # Display cart cont = 0 for i in carrito: print(f"[{cont + 1}] {i[0]}({i[1]}) * {i[2]} = {i[3]}") cont += 1 producto = int(input("Ingrese el id del producto que quieres eliminar =>")) producto = (producto - 1) del carrito[producto]
try: while True: # All menu logic hereexcept ValueError: print("Ingresaste un valor invalido")
Why wrap everything in try-except?
The try-except block catches ValueError exceptions that occur when users enter non-numeric input for operations expecting integers (like menu choices or quantities). This prevents the program from crashing unexpectedly.
==================================================|| Carrito de compras ||==================================================-------------------------------------------------- MENU --------------------------------------------------[1] Registrar usuario o actualizar datos usuario[2] Agregar producto al carrito de compras[3] Listar el carrito de compras[4] Actualizar carrito de compras[5] Eliminar producto en carrito de compras[6] Finalizar programa->1--------------------------------------------------Ingrese su nombre(s) =>JuanIngrese su apellido(s) =>PerezIngrese su numero de documento=>12345678Usuario registrado correctamente->2--------------------------------------------------Productos disponibles--------------------------------------------------[1] arroz lb - (4000)[2] azucar lb - (4500)[3] platano lb - (1000)[4] pollo lb - (6000)...Ingrese el id del producto que quieres agregar =>1 ¿Cuantas cantidades de 'arroz lb' quieres?(Ejm:7)-> 2PRODUCTO AGREGADO AL CARRITO->3-------------------------------------------------------------------------------------carrito actual-----------------------------------arroz lb(4000) * 2 = 8000-----------------------------------SUBTOTAL = 8000IVA 19% = 1520.0TOTAL = 9520.0-----------------------------------